# -*- coding: utf-8 -*-

# Created on 07/08/2010

# Copyright (C) 2010  Sergio Rodrigo

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import pygame

class ActorGraphic(pygame.sprite.Sprite):
    """ Graphic of the game actors.
    
    * Methods:
    __init__(self, bitmapName="", actor=None)
    update(self)
    _correctPosition(self)
    draw(self, screen)
    
    * Member variables:
    bitmapName -- String name of the bitmap
    actor -- Actor linked to this graphic
    rect -- Rect object of the graphic
    width -- Width of the graphic
    height -- Height of the graphic
    screen -- Screen (scene) where the graphic is displayed
    
    """
    
    def __init__(self, bitmapName):
        """ Constructor.
        
        * Arguments:
        bitmapName -- String name of the bitmap (default "")
        actor -- Actor linked to this graphic (default None)
        
        """
        pygame.sprite.Sprite.__init__(self)
        #self.image, self.rect = load_image(bitmapName)
        self.bitmap = pygame.image.load(bitmapName)
        self.actor = None
        self.rect = self.bitmap.get_rect()
        self.width = self.rect.width
        self.height = self.rect.height
        self.rect.centerx = self.width/2
        self.rect.centery = self.height/2
        self.screen = None

    def update(self):
        """ Updates the graphic and corrects its position on the scene.
        
        """
        self.rect.centerx = self.actor.x
        self.rect.centery = self.actor.y
        self._correctPosition()

    def _correctPosition(self):
        """ Corrects the position of the actior if it's excedes the limits of \
        the scene (screen).
        TODO: Cambiar esto. No tiene sentido, aunque funcione.. Repasar el sistema de coordenadas.
        
        """
        if (self.rect.centerx <= 0):
            #self.x = 0
            self.actor.x = 0
            self.rect.centerx = 0
        elif (self.rect.centerx + self.width >= self.screen.get_width()):
            #self.x = _width
            self.actor.x = self.screen.get_width() - self.width
            self.rect.centerx = self.screen.get_width() - self.width
        elif (self.rect.centery <= 0):
            #self.y = 0
            self.actor.y = 0
            self.rect.centery = 0
        elif (self.rect.centery + self.height >= self.screen.get_height()):
            #self.y = _height
            self.actor.y = self.screen.get_height() - self.height
            self.rect.centery = self.screen.get_height() - self.height

    def draw(self, screen):
        """ Draws the graphic on the scene (screen).
        
        """
        screen.blit(self.bitmap, (self.rect.centerx, self.rect.centery))

# eof
